/**
 * zmn角色模块-角色列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author heguanghua
 * @since 2020/04/23 13:45
 */
layui.define(['index', 'form', 'table', 'zmnApp', 'laypage', 'layedit', 'laydate','zmnSelect','treeGrid'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let layedit = layui.layedit;
    let laydate = layui.laydate;
    let zmnApp = layui.zmnApp;
    let zmnSelect = layui.zmnSelect;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-140'
    });

    laypage.render(zmnPage.laypageOptions(json));

        // 部门下拉组件
        let deptTree = function (orgId) {
            if (orgId == null) {
                orgId = 0; // 后端Integer类型接收
            }
            zmnSelect.deleteXmSelect("deptId");
            var objMap = zmnSelect.select({
                'deptId': {
                    isRadio: true, //单选标识：true-单选；false-多选
                    selectedValues: [json.vo.deptId == null ? '' : json.vo.deptId], //已选中值（用于回显)
                    tree: {
                        enable: true,
                        //是否展示三角图标
                        showFolderIcon: true,
                        //非严格模式
                        strict: false,
                    },
                    propMap: {  //选项参数映射配置
                        name: "deptName",   //显示值
                        value: "deptId"  //选中值
                    },
                    remote: { //远程获取下拉数据
                        enable: true, //开关：true-开启；false-关闭
                        url: '/common/dept/listDeptTree.action?orgId=' + orgId, //服务url
                    },
                }
            });
            objMap['deptId'].update({
                filterable: false,
                autoRow: false,
                model: {label: {type: 'text'}},
                on: function (data) {
                    if (data.isAdd) {
                        return data.change.slice(0, 1)
                    }
                }
            })
        }

        // 加载部门组件 回显部门数据
        if (appUtil.notNull(json.orgId)) {
            deptTree(json.orgId);
        } else if (appUtil.notNull(json.orgTwoId)) {
            deptTree(json.orgTwoId);
        } else {
            deptTree(json.plat);
        }

    // 启用/停用（批量）
    table.on("toolbar(zmn-table)", function (obj) {
        let ids = zmnPage.layTabldIds(obj, "roleId");
        if(obj.event != "start" && obj.event != "stop") { return; }
        if(ids.length === 0){ return layer.alert("请选择至少一个要操作的角色！"); }
        zmnPage.layStartStopBatch(obj, "/role/modify/modifyStatus.action", ids.join(","));
    });

    // 启用/停用（单个）
    form.on('switch(chk_status)', function(obj){
        zmnPage.layStartStop(obj, zmnApp.SYS_BASE_URL +"/role/modify/modifyStatus.action");
    });

    // 修改排序
    table.on("edit(zmn-table)", function(obj){
        zmnPage.laySort(obj, zmnApp.SYS_BASE_URL +"/role/modify/modifySort.action", obj.data.roleId);
    });

    layer.ready(function(){
        if(json.status){
            $("#status").val(json.status);
            form.render();
        }
    });

    /**
     * 复制
     */
    roleCopy = function () {
             var ids = table.checkStatus('zmn-table').data.map(item => item.roleId);
             var idsStr = ids.join(',');
             if (!idsStr) {
                layer.msg('请至少选择一个要操作的角色', {icon: 2, time: 1000});
                return;
             }
             layer.open({
                        type: 1,
                        title: "复制角色权限",
                        area: ['570px', '200px'],
                        content: $('#roleCopyTmp'),
                        btn: ['保存'],
                        yes: function (index, layero, obj) {
                          var ids = table.checkStatus('zmn-table').data.map(item => item.roleId);
                            var roleId = $("#roleId").val();
                            var idsStr = ids.join(',');
                            if (!idsStr) {
                                layer.msg('请至少选择一个要操作的角色', {icon: 2, time: 1000});
                                return;
                            }
                            $.ajax({
                                async: false,
                                type: "POST",
                                url: zmnApp.SYS_BASE_URL + "/role/modify/copy.action",
                                dataType: "json",
                                data: {"roleId": roleId, "ids": idsStr},
                                contentType: "application/x-www-form-urlencoded",
                                success: function (json) {
                                    if (json.status == 200) {
                                        layer.msg('复制成功！', {icon: 1, time: 1000});
//                                        $(".layui-layer-btn0").css("display", "none");
//                                        $("#roleSet").css("display", "none");
//                                        $("#roleId").val("");
                                        setTimeout(function () {
                                            window.location.reload();
                                        }, 1500)

                                    } else {
                                        layer.msg(json.message, {icon: 2, time: 1000});
                                    }
                                    return false;
                                }
                            });
                        },
                        cancel: function (index, layero) {
//                            $("#roleId").val("");
                            $(".layui-layer-btn0").css("display", "block");
                            $("#roleSet").css("display", "block");
                            $("#roleCopyTmp").css("display", "none");
                        }
                    })
    }

    //对外暴露的接口
    exports('role_list', {});
});
